package com.gtfrank.web.servlet.day2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.SocketOption;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @ProjectName: sudacoursetrainging
 * @ClassName: ShowParamListServlet
 * @PackageName:com.gtfrank.web.servlet.day2
 * @Description:
 * @Author: frank
 * @Date: 2021/4/28 15:33
 * @Version v1.0
 */
@WebServlet(value = "/servlet/day2/ShowParamListServlet")
public class ShowParamListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
       // String name = request.getParameter("userName");
        Map map =  request.getParameterMap();
        Set<Map.Entry> set = map.entrySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            Map.Entry entry = (Map.Entry)iterator.next();
            String key =(String) entry.getKey();
            String[] values = (String[])entry.getValue();
            out.println(key+"="+showArrayValue(values,","));
            out.println("<br>");
        }
        out.flush();
        out.close();
    }
    public String showArrayValue(String[] args,String str){
        StringBuffer jpsb = new StringBuffer();
        for (int i=0;i<args.length;i++){
            if(i!=0){
                jpsb.append(str);
            }
            jpsb.append(args[i]);
        }
        return jpsb.toString();
    }
}
